using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Markup;
using Rhizome.Contract;
using Rhizome.Contract.Visitor;
using Rhizome.Internal;

namespace Rhizome.Model
{
    [ContentProperty("Triggers")]
    public class AggregateTrigger<TPlant> : TriggerBase<TPlant>, IForwardEvent
    {
        public AggregateTrigger()
        {
            Triggers = new RhizomeElementCollection<ITrigger>(this);
        }

        public AggregateTrigger(params ITrigger[] triggers) : this()
        {
            foreach (var trigger in triggers)
                Triggers.Add(trigger);
        }

        public Collection<ITrigger> Triggers { get; private set; }

        #region IForwardEvent Members

    	public sealed override object GetForwardEvent()
        {
        	return Triggers.GetForwardEvent();
        }

        #endregion

        public override void Initialize()
        {
            Triggers.ForEachOfType<IStateInitialize>(t => t.Initialize());
        }

        public override void Cleanup()
        {
            Triggers.ForEachOfType<IStateInitialize>(t => t.Cleanup());
        }

    	#region Overrides of RhizomeElement

    	protected override IEnumerable<IEnumerable> GetElementCollections()
    	{
    		return base.GetElementCollections().Concat(new[] {Triggers});
    	}

    	#endregion
    }

    public sealed class AggregateTrigger : AggregateTrigger<object>
    {
    }
}